package io.renren.modules.sys.controller;

import java.util.List;
import java.util.Map;

import io.renren.common.utils.*;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.SysRechargeEntity;
import io.renren.modules.sys.service.SysRechargeService;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.user;


/**
 * 用户充值记录表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-11-08 19:36:41
 */
@RestController
@RequestMapping("/sys/sysrecharge")
public class SysRechargeController extends AbstractController{

	@Autowired
	private SysRechargeService sysRechargeService;

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:sysrecharge:list")
	public R list(@RequestParam Map<String, Object> params){

		if (getUserId() != Constant.SUPER_ADMIN && hasRole((long)Constant.NORMAL_USER)) {
			params.put("userId", getUserId());
		}

		//查询列表数据
        Query query = new Query(params);

		List<SysRechargeEntity> sysRechargeList = sysRechargeService.queryList(query);
		int total = sysRechargeService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(sysRechargeList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{rechargeId}")
	@RequiresPermissions("sys:sysrecharge:info")
	public R info(@PathVariable("rechargeId") Long rechargeId){
		SysRechargeEntity sysRecharge = sysRechargeService.queryObject(rechargeId);
		
		return R.ok().put("sysRecharge", sysRecharge);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:sysrecharge:save")
	public R save(@RequestBody SysRechargeEntity sysRecharge){

		ValidatorUtils.validateEntity(sysRecharge, AddGroup.class);

		SysUserEntity queryUser = new SysUserEntity();
		BeanUtils.copyProperties(sysRecharge, queryUser);
		SysUserEntity sysUserEntity = sysUserService.queryByEntity(queryUser);
		if (sysUserEntity == null) {
			return R.error("该用户不存在！");
		}

		sysRecharge.setUserId(sysUserEntity.getUserId());
		sysRecharge.setRealName(sysUserEntity.getRealName());

		sysRechargeService.save(sysRecharge);

		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:sysrecharge:update")
	public R update(@RequestBody SysRechargeEntity sysRecharge){
		//TODO 修改暂时不实现
//		sysRechargeService.update(sysRecharge);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:sysrecharge:delete")
	public R delete(@RequestBody Long[] rechargeIds){
		sysRechargeService.deleteBatch(rechargeIds);

		return R.ok();
	}
	
}
